package com.example.yls.demoa;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private Button btnquery;
    private TextView txtresult;
    private EditText addno;
    private EditText addname;
    private EditText addage;
    private Button addbtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtresult=findViewById(R.id.result);
        btnquery=findViewById(R.id.btn_query);
        btnquery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://com.gdcp.student/student");
                Cursor cursor = getContentResolver().query(uri, null, null, null, null);
                if(cursor != null && cursor.getCount() > 0){
                    StringBuffer buffer = new StringBuffer();
                    for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
                        String stuno = cursor.getString(0);
                        String name = cursor.getString(1);
                        int age = cursor.getInt(2);
                        buffer.append(stuno).append("  ").append(name).append("  ").append(age).append("\n");
                    }

                    txtresult.setText(buffer);
                }
            }
        });
        addno=findViewById(R.id.add_no);
        addage=findViewById(R.id.add_age);
        addname=findViewById(R.id.add_name);
        addbtn=findViewById(R.id.btn_add);
        addbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String no=addno.getText().toString();
                String name=addname.getText().toString();
                int age=Integer.parseInt(addage.getText().toString());
                Uri uri = Uri.parse("content://com.gdcp.student/student");
                ContentValues values = new ContentValues();
                values.put("stuno", no);
                values.put("name", name);
                values.put("age", age);
                getContentResolver().insert(uri, values);
            }
        });
    }
}
